"use strict";
cc._RF.push(module, '3957cmNSF5CaohvsIYsfrks', 'gameHttpModule');
// scripts/engines/modules/gameHttpModule.ts

"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
var superModule_1 = require("./superModule");
var GameHttpModule = /** @class */ (function (_super) {
    __extends(GameHttpModule, _super);
    function GameHttpModule() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.m_className = '网络http模块';
        //URL
        _this.GAME_SERVER_URL = 'http://api.xiayiqu.com';
        return _this;
    }
    GameHttpModule.prototype.PingPong = function (cb, errorCb) {
        // this.sendHttp(web_api.POST.ping_pong, 'post', null, cb, errorCb);
    };
    /**
     * 获取用户信息
     * @param wxIdentify 唯一标识
     * @param cb
     * @param errorCb
     */
    GameHttpModule.prototype.getUserWebInfo = function (wxIdentify, cb, errorCb) {
    };
    GameHttpModule.prototype.getLocalPropertyInfo = function (cb, errorCb) {
        var resData = {};
        this.sendHttp('/dataCenter/api/ip/getClientInfo', 'get', null, resData, cb, function (res) {
            console.error('#38 http错误', res);
            errorCb && errorCb(res);
        });
    };
    /***
     * 发送http条文
     * @param router 无需URL 路径
     * @param method 提交方式 GET POST
     * @param params 参数对象
     * @param resData 返回数据
     * @param successCallback 提交成功
     * @param errorCallback 提交失败
     * @param async 是否异步 - 默认 true
     */
    GameHttpModule.prototype.sendHttp = function (router, method, params, resData, successCallback, errorCallback, async) {
        var _this = this;
        if (async === void 0) { async = true; }
        var url = this.GAME_SERVER_URL + router;
        var data = null;
        if (params) {
            //
            for (var k in params) {
                var v = params[k];
                if (data == null) {
                    data = k + "=" + v;
                }
                else {
                    data += "&" + k + "=" + v;
                }
            }
            //
        }
        var xhr = new XMLHttpRequest();
        xhr.timeout = 10000;
        xhr.onreadystatechange = function () {
            if (xhr.readyState == XMLHttpRequest.DONE) {
                if (xhr.status >= 200 && xhr.status < 300) {
                    var response = xhr.responseText;
                    try {
                        _this.onHttpDone(JSON.parse(response), function (res) {
                            switch (typeof res) {
                                case "object": {
                                    successCallback(res);
                                    break;
                                }
                                case "string": {
                                    successCallback(JSON.parse(res));
                                    break;
                                }
                            }
                        }, errorCallback, xhr);
                    }
                    catch (e) {
                        console.error('运行后端回执引发的异常', response, e);
                    }
                }
                else {
                    errorCallback && errorCallback(null);
                }
            }
        };
        xhr.ontimeout = function () {
            console.error('请求超时', url);
            errorCallback && errorCallback({ code: -1, msg: '请求超时', data: null });
        };
        if (method.toUpperCase() == 'GET' && data) {
            url += "?" + data;
        }
        xhr.open(method, url, async);
        xhr.setRequestHeader('Content-type', 'application/json');
        if (method.toUpperCase() == 'POST') {
            xhr.send(JSON.stringify(params));
        }
        else {
            xhr.send();
        }
    };
    /**
     * @param {HttpData} jsonResponse
     * */
    GameHttpModule.prototype.onHttpDone = function (jsonResponse, successCallback, errorCallback, xhr) {
        var code = jsonResponse.code, msg = jsonResponse.msg, data = jsonResponse.data;
        if (code != null && code != 0) {
            errorCallback && errorCallback(jsonResponse);
            console.error('错误的回执', jsonResponse);
            return;
        }
        //
        successCallback(data || jsonResponse);
    };
    return GameHttpModule;
}(superModule_1.default)); //电子邮件puhalskijsemen@gmail.com
exports.default = GameHttpModule;
//源码网站 开vpn全局模式打开 http://web3incubators.com/
//电报https://t.me/gamecode999

cc._RF.pop();